{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Binary Number with Alternating Bits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: hasAlternatingBits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #交替位二进制数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个正整数，检查它的二进制表示是否总是 0、1 交替出现：换句话说，就是二进制表示中相邻两位的数字永不相同。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 5\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>5 的二进制表示是：101\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 7\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>7 的二进制表示是：111.</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 11\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>11 的二进制表示是：1011.</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [binary-number-with-alternating-bits](https://leetcode.cn/problems/binary-number-with-alternating-bits/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [binary-number-with-alternating-bits](https://leetcode.cn/problems/binary-number-with-alternating-bits/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5', '7', '11']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        str_n = str(bin(n))[2:]\n",
    "        for i in range(len(str_n)-1):\n",
    "            if str_n[i] == str_n[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        flag=1\n",
    "        while n>0:\n",
    "            a=n%2\n",
    "            b=(n//2)%2\n",
    "            if a!=b:\n",
    "                n=n//2\n",
    "            else:\n",
    "                flag=0\n",
    "                break\n",
    "        return bool(flag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        temp=7\n",
    "        while n:\n",
    "            if temp==n%2:\n",
    "                return False\n",
    "            temp=n%2\n",
    "            n//=2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=n^(n>>1)\n",
    "        b=a&(a+1)\n",
    "        return b==0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        temp=[]\n",
    "        while n>0:\n",
    "            temp.append(n%2)\n",
    "            n//=2\n",
    "        temp=temp[::-1]\n",
    "        length=len(temp)\n",
    "        for i in range(0,length-1):\n",
    "            if temp[i]==temp[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        # 将n右移一位，表示为(n >> 1)\n",
    "        # 和原来的n进行异或运算\n",
    "        a = n ^ (n >> 1)\n",
    "        # print(bin(a)[2:])\n",
    "        # 最后返回 a 应该是一个全1的二进制数\n",
    "        # (a+1) 则是 1 加若干个 0\n",
    "        return a & (a + 1) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        nbin=str(bin(n))\n",
    "        flag=True\n",
    "        for i in range(2,len(nbin)-1):\n",
    "            if nbin[i]==nbin[i+1]:\n",
    "                flag=False\n",
    "        return flag\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        prev = 2\n",
    "        while n:\n",
    "            cur = n % 2\n",
    "            if cur == prev:\n",
    "                return False\n",
    "            prev = cur\n",
    "            n //= 2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        prev = 2\n",
    "        while n:\n",
    "            cur = n % 2\n",
    "            if cur == prev:\n",
    "                return False\n",
    "            prev = cur\n",
    "            n //= 2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        return not (a := n ^ (n >> 1)) & (a + 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        n = bin(n)\n",
    "        print(n)\n",
    "        if '00' not in n and '11' not in n:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        # 判断n的二进制是否为1010交替的\n",
    "        # 就相当于判断n和n>>1(n的右移一位操作)的异或结果是否为全1\n",
    "        # 判断一个数a是否为全1，就相当于判断a和a+1相与是否为0。为0就是全1，不为0就不是全1\n",
    "        return not (a := n ^ (n >> 1)) & (a + 1) #整个结果，为0就表示是True，所以前面有not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        biao=9\n",
    "        while n>0:\n",
    "            yu=n%2\n",
    "            if yu==biao:\n",
    "                return False\n",
    "            biao=yu\n",
    "            n//=2\n",
    "        return True        \n",
    "                \n",
    "        \n",
    "\n",
    "       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        li = list(bin(n)[2:]) + ['?']\n",
    "        for i in range(len(li)-1):\n",
    "            if li[i] == li[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        if n == 0 or n == 1:\n",
    "            return True\n",
    "        devided = n\n",
    "        record = []\n",
    "        while (devided != 1 and devided != 0):\n",
    "            record.append(devided % 2)\n",
    "            if len(record)>=2:\n",
    "                if record[-1] == record[-2]:\n",
    "                    return False\n",
    "            devided = devided // 2\n",
    "        if devided == record[-1]:\n",
    "            return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        prev = 2\n",
    "        while n:\n",
    "            cur = n % 2\n",
    "            if cur == prev:\n",
    "                return False\n",
    "            prev = cur\n",
    "            n //= 2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        s=bin(n)[2:]\n",
    "        if '00' in s or '11' in s:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "\n",
    " \n",
    "                    \n",
    "        \n",
    "            \n",
    "            \n",
    "\n",
    "        \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        n0=bin(n)\n",
    "        for i in range(2,len(n0)-1):\n",
    "            if n0[i]==n0[i+1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        num1 = 2\n",
    "        while (n):\n",
    "            num2 = n%2\n",
    "            if num1==num2:\n",
    "                return False\n",
    "            num1 = num2\n",
    "            n = n//2\n",
    "        return True\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        while n>=1:\n",
    "            x=n%2\n",
    "            n=n//2\n",
    "            if x==n%2:\n",
    "                return False\n",
    "                break\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1);\n",
    "        return (a & (a + 1)) == 0;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        bit=n%2\n",
    "        n=n//2\n",
    "        while n>0:\n",
    "            temp=n%2\n",
    "            if temp==bit:\n",
    "                return False\n",
    "            else:\n",
    "                bit=temp\n",
    "            n=n//2\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        prev = 2\n",
    "        while n:\n",
    "            cur = n % 2\n",
    "            if cur == prev:\n",
    "                return False\n",
    "            prev = cur\n",
    "            n //= 2\n",
    "        return True\n",
    "\n",
    "        \n",
    "\n",
    "        \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        temp = n>>1\n",
    "        res = temp^n\n",
    "        flag = res & (res + 1)\n",
    "        return flag == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        num1=2\n",
    "        while n:\n",
    "            a=n%2\n",
    "            if a==num1:\n",
    "                return(False)\n",
    "            num1=a\n",
    "            n=n//2\n",
    "        return(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1)\n",
    "        return a & (a + 1) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        pre = n & 1\n",
    "        while n:\n",
    "            if n >> 1 & 1 == pre:\n",
    "                return False\n",
    "            n >>= 1\n",
    "            pre = n & 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        num=bin(n)[2:]\n",
    "        for i in range(len(num)-1):\n",
    "            if num[i]==num[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        num=0\n",
    "        i=0\n",
    "        while n>0:\n",
    "            num=(n%2)*10**i+num\n",
    "            n=n//2\n",
    "            i+=1\n",
    "        lst=list(map(int,str(num)))\n",
    "        for j in range(0,len(lst)-1):\n",
    "            if lst[j]==lst[j+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        while n:\n",
    "            if n & 3 in [1, 2]:\n",
    "                 n >>= 1\n",
    "            else:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=b=s=0\n",
    "        d=1\n",
    "        if n%2==1:\n",
    "            while s<n:\n",
    "                b=2**a\n",
    "                a+=2\n",
    "                s+=b\n",
    "            if s==n:\n",
    "                return True\n",
    "                    \n",
    "            else:return False\n",
    "        else:\n",
    "            while s<n:\n",
    "                b=2**d\n",
    "                d+=2\n",
    "                s+=b\n",
    "            if s==n:\n",
    "                return True\n",
    "            else: return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        while n > 0:\n",
    "            if n&1 != (n >> 1)&1:\n",
    "                n = n >> 1\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        prev = 2\n",
    "        while n:\n",
    "            cur = n % 2\n",
    "            if cur ==prev:\n",
    "                return False\n",
    "            prev=cur\n",
    "            n //= 2\n",
    "        return True\n",
    "\n",
    "        \n",
    "\n",
    "        \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1)\n",
    "        return a & (a + 1) == 0\n",
    "       \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef hasAlternatingBits(self, n):\n",
    "\t\tfor i in range(1, 32):\n",
    "\t\t\tif (1 << i) > n: break\n",
    "\t\t\ta = [1 if n & (1 << (i - 1)) else 0, 1 if n & (1 << i) else 0, 1 if n & (1 << (i + 1)) else 0]\n",
    "\t\t\tif a[1] == a[0] or a[1] == a[2]: return False\n",
    "\t\treturn True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        t_l = list(bin(n))\n",
    "        for i in range(2, len(t_l) - 1):\n",
    "            if t_l[i] == t_l[i + 1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        lst = []\n",
    "        while 1:\n",
    "            if n != 0:\n",
    "                m = n % 2\n",
    "                n //= 2\n",
    "                lst.append(m)\n",
    "            else:\n",
    "                break\n",
    "        lst1 = lst[::-1]\n",
    "        for i in range(len(lst1) - 1):\n",
    "            if lst1[i] == lst1[i + 1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        binary = bin(n)[2:]\n",
    "        for i in range(1, len(binary)):\n",
    "            if binary[i] == binary[i-1]:\n",
    "               return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=bin(n)\n",
    "        for i in range(len(a)-1):\n",
    "            if a[i]==a[i+1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        if n % 2 == 0:\n",
    "            # 1010 2+2*4+2*4*4\n",
    "            tmp = 2\n",
    "            while n > 2:\n",
    "                n -= tmp * 4\n",
    "                tmp *= 4\n",
    "            return True if n == 2 else False\n",
    "            \n",
    "        else:\n",
    "            # 101  1+ 4\n",
    "            n -= 1\n",
    "            tmp = 1\n",
    "            while n > 0:\n",
    "                n -= tmp * 4\n",
    "                tmp *= 4\n",
    "            return True if n == 0 else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "\n",
    "        return not ((a := n ^ (n >> 1)) & (a + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=bin(n)[2:]\n",
    "        for i in range(len(a)-1):\n",
    "            if a[i]==a[i+1]:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        s = bin(n)\n",
    "        for i in range(3, len(s)):\n",
    "            if s[i] == s[i-1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        # 将整数转换为二进制字符串\n",
    "        binary_str = bin(n)[2:]\n",
    "        \n",
    "        # 遍历二进制字符串，检查相邻两位是否相同\n",
    "        for i in range(len(binary_str) - 1):\n",
    "            if binary_str[i] == binary_str[i + 1]:\n",
    "                return False\n",
    "        \n",
    "        return True\n",
    "\n",
    "# 测试用例1：期望输出 True\n",
    "test_case_1 = Solution().hasAlternatingBits(5)\n",
    "print(test_case_1)\n",
    "\n",
    "# 测试用例2：期望输出 False\n",
    "test_case_2 = Solution().hasAlternatingBits(7)\n",
    "print(test_case_2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=bin(n)\n",
    "        for i in range(len(a)-1):\n",
    "            if a[i]==a[i+1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        t=n%2\n",
    "        n//=2\n",
    "        while(n):\n",
    "            k=n%2\n",
    "            if k^t:\n",
    "                t=k\n",
    "                n//=2\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        new_str=bin(n)[2:]\n",
    "        for i in range(len(new_str)-1):\n",
    "            if new_str[i]==new_str[i+1]:\n",
    "                return False\n",
    "\n",
    "        return True        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        n = bin(n)[2:]\n",
    "        for i in range(1, len(n)):\n",
    "            if int(n[i]) - int(n[i - 1]) == 0:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        binary = bin(n)[2:]\n",
    "        for i in range(1, len(binary)):\n",
    "            if binary[i] == binary[i-1]:\n",
    "               return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        if n & 1:\n",
    "            n >>= 1\n",
    "        while n:\n",
    "            r = n & 3\n",
    "            if r == 2:\n",
    "                n >>= 2\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = bin(n)\n",
    "        for i in range(len(a)-1):\n",
    "            if a[i]==a[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1)\n",
    "        return (a & a + 1) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        s = bin(n)[2:]\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        return not (a := n ^ (n >> 1)) & (a + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        yu = n%2\n",
    "        t = yu\n",
    "        n = n//2\n",
    "        while n != 0:\n",
    "            yu = n%2\n",
    "            if yu == t:\n",
    "                return False\n",
    "            t = yu\n",
    "            n //= 2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1)\n",
    "        return a & (a + 1) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1)\n",
    "        return a & (a + 1) == 0  # 如果全1，+1后会全0，看是不是为0就行\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        return not (a := n ^ (n >> 1)) & (a + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=b=s=0\n",
    "        d=1\n",
    "        if n%2==1:\n",
    "            while s<n:\n",
    "                b=2**a\n",
    "                a+=2\n",
    "                s+=b\n",
    "            if s==n:\n",
    "                return True\n",
    "                    \n",
    "            else:return False\n",
    "        else:\n",
    "            while s<n:\n",
    "                b=2**d\n",
    "                d+=2\n",
    "                s+=b\n",
    "            if s==n:\n",
    "                return True\n",
    "            else: return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        binary = bin(n)[2:]  # 将 n 转换为二进制字符串\n",
    "        for i in range(1, len(binary)):\n",
    "            if binary[i] == binary[i-1]:\n",
    "               return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = n ^ (n >> 1)\n",
    "        return a & (a + 1) == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        tmp = str(bin(n)[2:])\n",
    "        \n",
    "        for index, value in enumerate(tmp[:-1]):\n",
    "            if value == tmp[index+1]:\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        binary_num = bin(n)\n",
    "        binary_str = str(binary_num)[2:]\n",
    "        for i in range(1, len(binary_str)):\n",
    "            if binary_str[i] == binary_str[i-1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        n0=bin(n)\n",
    "        for i in range(2,len(n0)-1):\n",
    "            if n0[i]==n0[i+1]:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        lst = []\n",
    "        flag = True\n",
    "        while n > 0:\n",
    "            lst.append(n % 2)\n",
    "            n = n // 2\n",
    "        for i in range(len(lst)-1):\n",
    "            if lst[i] == lst[i+1]:\n",
    "                flag = False\n",
    "                break\n",
    "            else: \n",
    "                continue\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        while n != 0:\n",
    "            if (n & 1) != (n & 2) >> 1:\n",
    "                n = n >> 1\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a = bin(n)\n",
    "        for i in range(len(a) - 1):\n",
    "            if a[i] == a[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        a=2\n",
    "        while n:\n",
    "            b=n%2\n",
    "            if b==a:\n",
    "                return False\n",
    "            a=b\n",
    "            n //= 2\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        n=bin(n)\n",
    "        s=str(n)\n",
    "        s=s[2::]\n",
    "        m=len(s)\n",
    "        if m==1:\n",
    "            return True\n",
    "        if m>1:\n",
    "            if s[0]=='0':\n",
    "                s1=s[::2]\n",
    "                s2=s[1::2]\n",
    "                if ('1' not in s1)and('0' not in s2):\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            if s[0]=='1':\n",
    "                s1=s[::2]\n",
    "                s2=s[1::2]\n",
    "                if ('0' not in s1)and('1' not in s2):\n",
    "                    return True\n",
    "                else:\n",
    "                    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        n=bin(n)\n",
    "        s=str(n)\n",
    "        s=s[2::]\n",
    "        m=len(s)\n",
    "        if m==1:\n",
    "            return True\n",
    "        if m==2:\n",
    "            if int(s[0])^int(s[1])==1:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        if m>2:\n",
    "            if s[0]=='0':\n",
    "                s1=s[::2]\n",
    "                s2=s[1::2]\n",
    "                if ('1' not in s1)and('0' not in s2):\n",
    "                    return True\n",
    "                else:\n",
    "                    return False\n",
    "            if s[0]=='1':\n",
    "                s1=s[::2]\n",
    "                s2=s[1::2]\n",
    "                if ('0' not in s1)and('1' not in s2):\n",
    "                    return True\n",
    "                else:\n",
    "                    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        return not (a := n ^ (n >> 1)) & (a + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        while n>1:\n",
    "            if n%2==n//2%2:\n",
    "                return False\n",
    "            else:\n",
    "                n=n//2\n",
    "        else:\n",
    "            return True        \n",
    "       \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasAlternatingBits(self, n: int) -> bool:\n",
    "        if n % 2 == 0:\n",
    "            # 1010 2+2*4+2*4*4\n",
    "            tmp = 2\n",
    "            while n > 2:\n",
    "                n -= tmp * 4\n",
    "                tmp *= 4\n",
    "            return True if n == 2 else False\n",
    "            \n",
    "        else:\n",
    "            # 101  1+ 4\n",
    "            n -= 1\n",
    "            tmp = 1\n",
    "            while n > 0:\n",
    "                n -= tmp * 4\n",
    "                tmp *= 4\n",
    "            return True if n == 0 else False"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
